﻿using MediatR;
using WMS_Goodsinfo.Api.Read.Application.Command;
using WMS_Goodsinfo.Api.Read.DTOS;
using WMS_Goodsinfo.Domain;
using WMS_Goodsinfo.ErrorCode;
using WMS_Goodsinfo.Infastructure;

namespace WMS_Goodsinfo.Api.Read.Application.Handler
{
    public class SelectBatchHandler : IRequestHandler<SelectBatchCommand, ApiPaging<BatchListDto>>
    {
        private readonly IProductRepository<Product_Table> productRepo;

        public SelectBatchHandler(IProductRepository<Product_Table> productRepo)
        {
            this.productRepo = productRepo;
        }
        /// <summary>
        /// 查询批次数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiPaging<BatchListDto>> Handle(SelectBatchCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<BatchListDto>result = new ApiPaging<BatchListDto>();

            var query = productRepo.GetAll().Where(p => !p.IsDelete);
             // 应用过滤条件
            if (!string.IsNullOrEmpty(request.ProductName))
            {
                query = query.Where(p => p.ProductName.Contains(request.ProductName));
            }

            if (!string.IsNullOrEmpty(request.ProductTypeId))
            {
                query = query.Where(p => p.ProductTypeId == request.ProductTypeId);
            }

            if (!string.IsNullOrEmpty(request.UnitName))
            {
                query = query.Where(p => p.UnitName.Contains(request.UnitName));
            }

            if (!string.IsNullOrEmpty(request.Inventory))
            {
                query = query.Where(p => p.Inventory.Contains(request.Inventory));
            }

            if (!string.IsNullOrEmpty(request.Batch))
            {
                query = query.Where(p => p.Batch.Contains(request.Batch));
            }

            if (request.Productiondate.HasValue)
            {
                query = query.Where(p => p.Productiondate.Date == request.Productiondate.Value.Date);
            }

            if (request.Expirationdate.HasValue)
            {
                query = query.Where(p => p.Expirationdate.Date == request.Expirationdate.Value.Date);
            }

            // 获取总数
            var totalCount = query.Count();

            // 分页查询
            var pageData = query
                .Skip((request.Pageindex - 1) * request.Pagesize)
                .Take(request.Pagesize)
                .Select(p => new BatchListDto
                {
                    ProductNo = p.ProductNo,
                    ProductName = p.ProductName,
                    ProductTypeId = p.ProductTypeId,
                    ProductModel = p.ProductModel,
                    Status = p.Status,
                    UnitName = p.UnitName,
                    Barcode = p.Barcode,
                    Inventory = p.Inventory,
                    Location = p.Location,
                    Current_Inventory = p.Current_Inventory,
                    TotalMoeny = p.TotalMoeny,
                    Remarks = p.Remarks,
                    Batch = p.Batch,
                    Productiondate = p.Productiondate,
                    Expirationdate = p.Expirationdate,
                    Shelflifedays = p.Shelflifedays,
                    Remainingdays = p.Remainingdays,
                    Alertdays = p.Alertdays
                })
                .ToList();

            result.Code = StatusEnum.OK;
            result.Message = "查询成功";
            result.TotalCount = totalCount;
            result.PageData = pageData;

            return Task.FromResult(result);

        }
    }
}
